using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows;
using Newtonsoft.Json;
using NUnit.Framework;
using RFS.BizLogic;
using RFS.BizLogic.PaiShi.Accounting;
using RFS.BizLogic.PaiShi.Ports;
using RFS.BizLogic.PaiShi.Ports.Adapters;
using RFS.BizLogic.Sys;
using RFS.BizLogic.utils;
using RFS.BizLogic.utils.impl;
using RFS.Domain.PaiShi;
using RFS.Domain.PaiShi.Accounting;
using RFS.WPF;
using RFS.WPF.StartUp;
using RFS.WPF.utils;
using RFS.WPF.Views;
using ycx;

namespace IntergrationTest.PaiShi
{
    public class ItAVSRepo
    {
        private IAntiObject _anti;
        private App a;

        [SetUp]
        public void setup()
        {
            _anti = Starter.StartApp(false);
            a = new App();
        }


        [Test]
        public void T03_Avs_CRUD_CorrectnessTest_givenAAVs_ShouldBeSaved()
        {
            var accitems = 2;
            var avitems = 1;

            _anti.Get<ILog>().NewLevedLogAdded += (sender, tuple) =>
            {
                if (tuple.l >= LogLevel.Info) tuple.Echo();
            };


            #region create a avs

            var avs = new DayBasedWorkFlowNodeAccountingVoucherSetWrapper()
            {
                Id = 1,
                AccPeriod = new AccountPeriod(DateTime.Parse("2021-1-1"), 2)
            };
            for (int j = 0; j < avitems; j++)
            {
                var avss = new List<OrderBasedAccItem>();
                for (int i = 0; i < accitems; i++)
                {
                    var ac = new OrderBasedAccItem()
                    {
                        Id = (ulong)(111 + i + j * 10000),
                        Summary = "b",
                        Account = Account.XSFY,
                        RelatedOrderItem = new OrderItem() { BizId = $"1111{i + j * 10000}", LocalId = 2 },
                        AmountDebit = new Amount(),
                        AmountCredit = new Amount(3, 3),
                        AmountLeft = new Amount(3, 3),
                    };
                    avss.Add(ac);
                }

                var av1 = new OrderItemBasedAccountingVoucher()
                {
                    Id = 11 + (ulong)j,
                    VType = VType.手动制单,
                    AccItems = avss
                };
                // vsset.Add(av1);
                avs.AddAv(av1);
            }

            #endregion

            var r = _anti.Get<IAVSRepoPort>();
            Thread.Sleep(50);
            r.Save(avs);

            var avsgot = r.GetById(avs.Id);
            avsgot.Vouchers.ElementAt(0).AccItems.Count().Echo("test:");
            Assert.AreEqual(avsgot.Vouchers.Count(), avitems);
            Assert.AreEqual(avsgot.Vouchers.ElementAt(0).AccItems.Count(), accitems);

            // avsgot.Echo();

            r.Delete(avs.Id);
            var ex = Assert.Throws<Exception>(() => r.GetById(avs.Id));
            ex.Message.Echo();
            Assert.True(ex.Message.Contains("找不到"));
        }

        [Test]
        public void T04_AvsCRUD_Presure_Test_givenAAVs_ShouldBeSaved()
        {
            var accitems = 2;
            var avitems = 1000;

            _anti.Get<ILog>().NewLevedLogAdded += (sender, tuple) =>
            {
                if (tuple.l >= LogLevel.Info) tuple.Echo();
            };

            #region create a avs

            var avs = new DayBasedWorkFlowNodeAccountingVoucherSetWrapper()
            {
                Id = 1,
                AccPeriod = new AccountPeriod(DateTime.Parse("2021-1-1"), 2)
            };
            for (int j = 0; j < avitems; j++)
            {
                var avss = new List<OrderBasedAccItem>();
                for (int i = 0; i < accitems; i++)
                {
                    var ac = new OrderBasedAccItem()
                    {
                        Id = (ulong)(111 + i + j * 10000),
                        Summary = "b",
                        Account = Account.XSFY,
                        RelatedOrderItem = new OrderItem() { BizId = $"1111{i + j * 10000}", LocalId = 2 },
                        AmountDebit = new Amount(),
                        AmountCredit = new Amount(3, 3),
                        AmountLeft = new Amount(3, 3),
                    };
                    avss.Add(ac);
                }

                var av1 = new OrderItemBasedAccountingVoucher()
                {
                    Id = 11 + (ulong)j,
                    VType = VType.手动制单,
                    AccItems = avss
                };
                // vsset.Add(av1);
                avs.AddAv(av1);
            }

            #endregion

            var r = _anti.Get<IAVSRepoPort>();
            try
            {
                Thread.Sleep(50);
                r.Save(avs);

                var avsgot = r.GetById(avs.Id);
                avsgot.Vouchers.ElementAt(0).AccItems.Count().Echo("test:");
                Assert.AreEqual(avsgot.Vouchers.Count(), avitems);
                Assert.AreEqual(avsgot.Vouchers.ElementAt(0).AccItems.Count(), accitems);
            }
            finally
            {
                r.Delete(avs.Id);
            }
        }


        [Test]
        public void T04_AvsIfTheDayCheded_()
        {
            var accitems = 2;
            var avitems = 1;
            var day = DateTime.Parse("2025-1-14");


            #region create a avs

            _anti.Get<ILog>().NewLevedLogAdded += (sender, tuple) =>
            {
                if (tuple.l >= LogLevel.Info) tuple.Echo();
            };


            var avs = new DayBasedWorkFlowNodeAccountingVoucherSetWrapper()
            {
                Id = 1,
                AccPeriod = new AccountPeriod(day, 1)
            };
            for (int j = 0; j < avitems; j++)
            {
                var avss = new List<OrderBasedAccItem>();
                for (int i = 0; i < accitems; i++)
                {
                    var ac = new OrderBasedAccItem()
                    {
                        Id = (ulong)(111 + i + j * 10000),
                        Summary = "b",
                        Account = Account.XSFY,
                        RelatedOrderItem = new OrderItem() { BizId = $"1111{i + j * 10000}", LocalId = 2 },
                        AmountDebit = new Amount(),
                        AmountCredit = new Amount(3, 3),
                        AmountLeft = new Amount(3, 3),
                    };
                    avss.Add(ac);
                }

                var av1 = new OrderItemBasedAccountingVoucher()
                {
                    Id = 11 + (ulong)j,
                    VType = VType.手动制单,
                    AccItems = avss
                };
                // vsset.Add(av1);
                avs.AddAv(av1);
            }

            #endregion

            var r = _anti.Get<IAVSRepoPort>();
            try
            {
                Thread.Sleep(50);
                r.Save(avs);
                r.GetTheLastesRecords(   3).Echo();
                
            }
            finally
            {
                r.Delete(avs.Id);
            }
        }
    }
}